package com.maqconfig.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maqconfig.mapper.app.AppMapper;
import com.maqconfig.model.app.APP;
import com.maqconfig.service.AppService;
import common.MQR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppServiceImpl extends ServiceImpl<AppMapper, APP> implements AppService {

    @Autowired
    private AppMapper appMapper;

    @Override
    public MQR saveOrUpdateApp(String id, String appName, String AppDescribe) {
        APP result = appMapper.selectById(id);
        if(result != null){
            result.setAppDescribe(AppDescribe);
            result.setAppName(appName);
            this.updateById(result);
            return MQR.OK(result);
        }else{
            APP app = new APP();
            app.setAppName(appName);
            app.setAppDescribe(AppDescribe);
            this.save(app);
            return MQR.OK(app);
        }
    }

    @Override
    public List<APP> findAppsByUser(String userId) {
        QueryWrapper<APP> queryWrapper = new QueryWrapper<APP>();
        queryWrapper.eq("USER_ID",userId);
        return appMapper.selectList(queryWrapper);
    }

    @Override
    public List<APP> findAllApp() {
        return appMapper.selectList(null);
    }

    @Override
    public IPage<APP> queryAppPageByUserId(String userId,IPage<APP> page) {
        QueryWrapper<APP> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(userId)){
            queryWrapper.eq("USER_ID",userId);
        }

        return appMapper.selectPage(page,queryWrapper);
    }

}
